package org.univie.mappr;

import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MetadataView extends Activity implements OnGestureListener {

	// debugging
	private static final String TAG = "org.univie.mappr.MetadataView";

	private FlickrPhoto mPhoto;
	private TableLayout mTableLayout;
	private GestureDetector gestureListener;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.metadata_view);

		mTableLayout = (TableLayout) findViewById(R.id.metadata_view_table_layout);
		/*
		 * mTableLayout.setShrinkAllColumns(true);
		 * mTableLayout.setStretchAllColumns(false);
		 * mTableLayout.setScrollBarStyle(ScrollView.SCROLLBARS_INSIDE_OVERLAY);
		 * mTableLayout.setScrollContainer(true);
		 */
		mTableLayout.setPadding(25, 25, 25, 0);
		mTableLayout.bringToFront();
		// gestureDetector
		gestureListener = new GestureDetector(this);
		// assign the photo to the one selected in the MapprView
		mPhoto = MapprView.sCurrentPhoto;
		Log.d(TAG, "behind getting photo");
		if (mPhoto == null)
			finish(); // TODO abort

		addMetadata();
	}

	private void addMetadata() {

		Vector<TableRow> tableRows = new Vector<TableRow>();
		Log.d(TAG, "in adding Metadata");
		TableRow tr = new TableRow(this);
		TableRow tr2 = new TableRow(this);
		TableRow tr3 = new TableRow(this);
		TableRow tr4 = new TableRow(this);
		TableRow tr5 = new TableRow(this);
		TableRow tr6 = new TableRow(this);
		TableRow tr7 = new TableRow(this);

		// TODO: making nicer layout for metadataview

		TextView viewTitle = new TextView(this);
		viewTitle.setText(R.string.metadata_view_title);
		tr.addView(viewTitle);
		viewTitle.append(": " + mPhoto.getTitle());

		tableRows.add(tr);
		tableRows.add(tr2);
		tableRows.add(tr3);
		tableRows.add(tr4);
		tableRows.add(tr5);
		tableRows.add(tr6);
		tableRows.add(tr7);

		TextView viewDesc = new TextView(this);
		viewDesc.setText(R.string.metadata_view_desc);
		viewDesc.setGravity(android.view.Gravity.LEFT);
		tr2.addView(viewDesc);
		viewDesc.append(": " + mPhoto.getDesc());

		TextView viewDatePosted = new TextView(this);
		viewDatePosted.setText(R.string.metadata_view_date_posted);
		tr3.addView(viewDatePosted);
		viewDatePosted.append(": " + mPhoto.getDatePosted().toGMTString());

		TextView viewDateTaken = new TextView(this);
		viewDateTaken.setText(R.string.metadata_view_date_taken);
		tr4.addView(viewDateTaken);
		viewDateTaken.append(": " + mPhoto.getDateTaken().toGMTString());

		TextView viewLocation = new TextView(this);
		viewLocation.setText(R.string.metadata_view_location);
		tr5.addView(viewLocation);
		viewLocation.append(": \n \t lat:"
				+ mPhoto.getLocation().getLatitudeE6() / 6E1 + "\n \t lon:"
				+ mPhoto.getLocation().getLongitudeE6() / 6E1);

		TextView viewTags = new TextView(this);
		viewTags.setText(R.string.metadata_view_tags);
		tr6.addView(viewTags);
		String tags = ": \n \t";
		for (String tag : mPhoto.getTags()) {
			tags += tag + "\n \t";
		}
		viewTags.append(tags);

		TextView viewOwner = new TextView(this);
		viewOwner.setText(R.string.metadata_view_owner);
		tr7.addView(viewOwner);
		viewOwner.append(":\n \t UserName: " + mPhoto.getOwnerUserName()
				+ "\n \t RealName: " + mPhoto.getOwnerRealName());
		Log.d(TAG, " adding Rows");
		for (TableRow tRow : tableRows) {
			Log.d(TAG, "adding row to view");
			tRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			// DEBUG
			// tRow.setBackgroundColor(Color.CYAN);
			mTableLayout.addView(tRow);
		}
		Log.d(TAG, "all should be added");
	}

	@Override
	public boolean onTouchEvent(MotionEvent me) {
		super.onTouchEvent(me);
		return gestureListener.onTouchEvent(me);

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		gestureListener.onTouchEvent(ev);
		super.dispatchTouchEvent(ev);
		return true;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {

		if (distanceX < -50) {
			// scroll left
			Log.d(TAG, "scrolled left");
			finish();
			return true;
		}
		if (distanceX > 0) {
			// scroll right
			Log.d(TAG, "scrolled right");
		}
		if (distanceY < -10) {
			// scroll up
			Log.d(TAG, "scrolled up");
			// mTableLayout.scrollBy(0, -10);
			return true;
		}
		if (distanceY > 10) {
			Log.d(TAG, "scrolled down");
			// scroll down
			// mTableLayout.scrollBy(0, 10);
			return true;
		}
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// Auto-generated method stub
		return false;
	}

}
